#!/bin/bash

# Script to check if any proxied web-site is in China
# If it is, add them to China Domain for a fast access

chnconf="/etc/squid3/domain.china"
output="./.output.query"

function find_key(){
  cat $output |\
  awk -F"[<>]" '{for (i=1;i<=NF;i++) print $i}' |\
  grep "$1"  |\
  awk -F":" '{ \
    if (NF >= 2) {\
      gsub("[ |\t]", "", $NF); \
      print $NF\
    } else {\
      cnt=split($0, parts, "." );\
      gsub("[ |\t]", "", parts[cnt]); \
      print parts[cnt]\
    }\
  }' | tail -n 1
}

function wgets(){
  H='--header'
  wget \
  $H='Accept-Language: en-us,en;q=0.5' \
  $H='Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8' \
  $H='Connection: keep-alive' \
  -U 'Mozilla/5.0 (Windows NT 5.1; rv:10.0.2) Gecko/20100101 Firefox/10.0.2' \
  --referer=http://www.askapache.com/ \
  --keep-session-cookies \
  --save-cookies /tmp/wget03_cookies.txt \
  "$@"
}

function get_domain_location(){
  dnsname="$1"
  [ "$dnsname" == "" ] && echo "Usage: $0 domain_name" && exit -1
  queryname=`echo "$dnsname" | awk -F"." '( NF >=2 ){printf("%s.%s\n", $(NF-1), $NF)}'`
  [ "$queryname" == "" ] && echo "bad domain format: $dnsname" && exit -2

#  echo "Query $queryname ......"
  rm -f $output
  export http_proxy="http://web-proxy.atlanta.hp.com:8088"
#  wgets "http://www.networksolutions.com/whois/results.jsp?domain=$queryname" -O $output 1>/dev/null 2>&1
  wgets "http://who.is/whois/$queryname" -O $output 1>/dev/null 2>&1
  [ $? -ne 0 ] && echo "Error while executing query" && exit -3

  country=`find_key "Registrant Country"`
  [ "$country" == "" ] && country=`find_key "country"`
  city=`find_key "Registrant City"`
  [ "$city" == "" ] && city=`find_key "city"`
  echo $country $city
  return
}


[ "$1" != "" ] && get_domain_location "$1" && exit 0

cat /var/log/squid3/access.log | awk '{\
  gsub("http[s]*://", "", $7);\
  cnt=split($7, parts, "/");\
  $7=parts[1];\
  cnt=split($7, parts, ":");\
  $7=parts[1];\
  cnt=split($7, parts, ".");\
  $7=parts[cnt-1]"."parts[cnt];\
  cnt=split($9, parts, "/");\
  $9=parts[cnt];\
  if ($9 != "-") {print $7"\t"$9} }' | sort -u | \
while read domain proxy
do
  # Only concern about proxy that should be routed to chn
  if [ "`echo $proxy | grep chn`" == "" ]
  then
    echo "# $domain -> $proxy"
    area=`get_domain_location $domain`
    echo "# $area"
    if [ "`echo $area | grep CN`" != "" -o "`echo $area | grep China`" != "" ]
    then
      echo "echo .$domain >> $chnconf"
    fi
  fi
done

echo "cat $chnconf | sort -u > $chnconf.1"
echo "mv $chnconf.1 $chnconf"

